package com.ruoyi.web.controller.market.special;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.db.domain.SpecialCostomUserInfo;
import com.ruoyi.db.domain.SpecialDataInfo;
import com.ruoyi.db.model.param.SpecialDataParam;
import com.ruoyi.special.model.vo.MemberRiskInfoVO;
import com.ruoyi.special.service.ISpecialCostomUserInfoService;
import com.ruoyi.special.service.ISpecialDataInfoService;
import com.ruoyi.special.service.ISpecialMemberService;
import com.ruoyi.web.service.IMemberInfoHandleService;
import com.ruoyi.web.service.ISpecialAgencyService;
import com.ruoyi.web.wapper.SpecialDataInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 渠道信息Controller
 * 
 * @author ruoyi
 * @date 2022-08-04
 */
@Controller
@RequestMapping("/special/agency/data")
public class SpecialAgencyDataController extends BaseController
{
    private String prefix = "special/agency/data";

    @Autowired
    ISpecialMemberService specialMemberService;
    @Autowired
    IMemberInfoHandleService memberInfoHandleService;
    @Autowired
    ISpecialDataInfoService specialDataInfoService;
    @Autowired
    ISpecialCostomUserInfoService specialCostomUserInfoService;
    @Autowired
    ISpecialAgencyService specialAgencyService;

    @RequiresPermissions("special:agency:data")
    @GetMapping("")
    public String dataList()
    {
        return prefix + "/dataList";
    }

    /**
     * 用户可见的列表
     * @param specialDataParam
     * @return
     */
    @RequiresPermissions("special:agency:data")
    @PostMapping("/dataListInfo")
    @ResponseBody
    public TableDataInfo dataListInfo(SpecialDataParam specialDataParam)
    {
        // 查询用户列表数据
        startPage();
        List<Map<String, Object>> mapList = specialMemberService.getCommonListData(specialDataParam);
        mapList = new SpecialDataInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    @RequiresPermissions("special:agency:data")
    @GetMapping("/dataView/{uid}")
    public String view(@PathVariable("uid") Long uid, ModelMap mmap)
    {
        SysUser sysUser = getSysUser();
        /**
         * 判断是否可以查询该用户的信息
         */
        Long deptId = sysUser.getDeptId();
        SpecialDataInfo dataInfo = specialAgencyService.getDataByDeptAndUid(uid, deptId);
        if (ObjectUtil.isEmpty(dataInfo)){
            return "无权限访问数据";
        }

        JSONObject userInfo = memberInfoHandleService.getMemberInfoDetail(uid);
        mmap.put("userInfo", userInfo);

        // 风控信息
        MemberRiskInfoVO memberRiskInfo = specialAgencyService.getMemberRiskInfo(uid, getSysUser());
        if (ObjectUtil.isNotEmpty(memberRiskInfo)){
            mmap.put("riskFlag", 1);
            mmap.put("memberRiskInfo", memberRiskInfo);
        }else{
            mmap.put("riskFlag", 0);
        }
        return prefix + "/dataView";
    }

    @RequiresPermissions("special:agency:data")
    @GetMapping("/View/{uid}")
    public String View(@PathVariable("uid") Long uid, ModelMap mmap)
    {

        // 风控信息
        MemberRiskInfoVO memberRiskInfo = specialAgencyService.getMemberRiskInfo(uid, getSysUser());
        if (ObjectUtil.isNotEmpty(memberRiskInfo)){
            mmap.put("riskFlag", 1);
            mmap.put("memberRiskInfo", memberRiskInfo);
        }else{
            mmap.put("riskFlag", 0);
        }

        return prefix + "/View";
    }


    /**
     * 修改特殊渠道数据
     */
    @RequiresPermissions("special:agency:data")
    @GetMapping("/dataEdit/{id}")
    public String dataEdit(@PathVariable("id") String id, ModelMap mmap)
    {
        SpecialDataInfo specialDataInfo = specialDataInfoService.selectSpecialDataInfoById(id);
        mmap.put("specialDataInfo", specialDataInfo);

        return prefix + "/dataEdit";
    }

    /**
     * 修改保存特殊渠道数据
     */
    @RequiresPermissions("special:agency:data")
    @PostMapping("/dataEditInfo")
    @ResponseBody
    public AjaxResult dataEditInfo(SpecialDataInfo specialDataInfo)
    {
        SpecialDataInfo dataInfo = specialDataInfoService.selectSpecialDataInfoById(specialDataInfo.getId());

        SysUser sysUser = ShiroUtils.getSysUser();
        String content = StrUtil.isNotEmpty(dataInfo.getFeedbackInfo())?dataInfo.getFeedbackInfo() + "</br>" :"";
        content = content + StrUtil.format("{} {}:{}", DateUtils.getDateTime(), sysUser.getUserName(), specialDataInfo.getFeedbackInfo());

        specialDataInfo.setFeedbackInfo(content);

        return toAjax(specialDataInfoService.updateSpecialDataInfo(specialDataInfo));
    }

    /**
     * 修改特殊渠道数据
     */
    @RequiresPermissions("special:agency:datagrant")
    @GetMapping("/dataGrant")
    public String dataEdit(@RequestParam("dataId") String dataId,
                           @RequestParam("userId") String userId,
                           ModelMap mmap)
    {

        Long deptId = getSysUser().getDeptId();

        List<SpecialCostomUserInfo> userInfoList = specialCostomUserInfoService.selectCostomUserInfoList(deptId);
        mmap.put("userInfoList", userInfoList);

        mmap.put("dataId", dataId);
        mmap.put("userId", userId);

        return prefix + "/dataGrant";
    }

    /**
     * 修改保存特殊渠道数据
     */
    @RequiresPermissions("special:agency:datagrant")
    @PostMapping("/dataGrantInfo")
    @ResponseBody
    public AjaxResult dataGrantInfo(@RequestParam("dataId") String dataId,
                                    @RequestParam("userId") String userId)
    {

        Long deptId = getSysUser().getDeptId();

        SpecialDataInfo updateEntity = new SpecialDataInfo();
        updateEntity.setId(dataId);
        updateEntity.setAdminUserId(Long.valueOf(userId));
        updateEntity.setDeptId(deptId);

        return toAjax(specialDataInfoService.updateSpecialDataInfo(updateEntity));
    }

}
